﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Sprites
{
    public class SpriteBatchInfo : Component
    {
        private enum BeginOverload
        {
            Empty,
            Blend,
            BlendSortSave,
            BlendSortSaveTransform,
        }

        private SpriteBatch spriteBatch;
        private BlendState blendMode;
        private SpriteSortMode sortMode;
        private Matrix transformMatrix;

        private bool inBatch;
        private BeginOverload beginOverload;

        public SpriteBatchInfo(GraphicsDevice graphicsDevice)
        {
            this.spriteBatch = new SpriteBatch(graphicsDevice);
        }

        public SpriteBatchInfo(GraphicsDevice graphicsDevice, BlendState blendMode)
            : this(graphicsDevice)
        {
            this.blendMode = blendMode;
            this.beginOverload = BeginOverload.Blend;
        }

        public SpriteBatchInfo(GraphicsDevice graphicsDevice, BlendState blendMode, SpriteSortMode sortMode)
            : this(graphicsDevice)
        {
            this.blendMode = blendMode;
            this.sortMode = sortMode;
            this.beginOverload = BeginOverload.BlendSortSave;
        }

        public SpriteBatchInfo(GraphicsDevice graphicsDevice, BlendState blendMode, SpriteSortMode sortMode, Matrix transformMatrix)
            : this(graphicsDevice)
        {
            this.blendMode = blendMode;
            this.sortMode = sortMode;
            this.transformMatrix = transformMatrix;
            this.beginOverload = BeginOverload.BlendSortSaveTransform;
        }

        public SpriteBatch Begin()
        {
            if (!this.inBatch)
            {
                switch (this.beginOverload)
                {
                    case BeginOverload.Empty:
                        this.spriteBatch.Begin();
                        break;
                    case BeginOverload.Blend:
                        this.spriteBatch.Begin(this.sortMode, this.blendMode);
                        break;
                    case BeginOverload.BlendSortSave:
                        this.spriteBatch.Begin(this.sortMode, this.blendMode);
                        break;
                    case BeginOverload.BlendSortSaveTransform:
                        this.spriteBatch.Begin(this.sortMode, this.blendMode, null, null, null, null, this.transformMatrix);
                        break;
                    default:
                        throw new InvalidOperationException();
                }
                this.inBatch = true;
            }
            return this.spriteBatch;
        }

        public void End()
        {
            if (this.inBatch)
            {
                this.spriteBatch.End();
                this.inBatch = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.spriteBatch != null)
                {
                    this.spriteBatch.End();
                    this.spriteBatch.Dispose();
                    this.spriteBatch = null;
                }
            }
            base.Dispose(disposing);
        }
    }
}
